package com.jsmtr.rms.controller.DUserResume;

import com.jsmtr.rms.entity.BaseUser;
import com.jsmtr.rms.entity.DUserResume;
import com.jsmtr.rms.entity.ProjectInfoFlow;
import com.jsmtr.rms.service.IDoResumeService;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 记录应聘者记过控制
 * @author: cyx
 * @description:
 * @date: Created in 2019/3/8 9:54
 * @modified by:
 */
@Controller
public class DUserResumeRActionController {
    @Resource
    private IUtilService utilService;

    @Resource
    private IDoResumeService doResumeService;

    @Resource
    private IProjectService projectService;

    @RequestMapping(value = "/DUserResume/RAction")
    public void RAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] uids = request.getParameterValues("uid");
            if (uids == null) {
                utilService.printJsonResult(false, "请选择人员！", "no", response);
            } else {
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码

                BaseUser cuser = utilService.getSessionUser(request);
                if (cuser.getUdept().equals(deptcode)) {
                    if (utilService.FlowIsDoing(fid)) {
                        List<DUserResume> editList = new ArrayList<DUserResume>();
                        List<DUserResume> users = doResumeService.getResumes(uids);
                        ProjectInfoFlow flow=projectService.getFolwById(fid);
                        for (DUserResume user : users) {
                            user=fillFowResult(user,request,flow,cuser);
                            editList.add(user);
                        }

                        if (editList.size() > 0) {
                            utilService.editList(editList);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中信息不支持此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败", "no", response);
        }
    }




    public DUserResume fillFowResult(DUserResume resume,HttpServletRequest request,ProjectInfoFlow nowflow,BaseUser operator){
        String flowName=nowflow.getFname();
        if (!StringTools.trimNotEmpty(flowName)){
            flowName=nowflow.getNickname();
        }
        String newstate=request.getParameter("state");
        String newremark=request.getParameter("remark");
        if (flowName.equalsIgnoreCase(EnumTools.FlowNomalState.简历筛选.name())){
            resume= fillResumeResult(resume,newstate,newremark,nowflow,operator);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.笔试.name())){
            resume=fillExamResult(resume,newstate,newremark,nowflow,operator);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.资料审核.name())){
            resume=fillCheckResult(resume,newstate,newremark,nowflow,operator);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.面试.name())){
            resume=fillInterviewResult(resume,newstate,newremark,nowflow,operator);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.背景调查.name())||flowName.equalsIgnoreCase("政审")){
            resume=fillPReviewResult(resume,newstate,newremark,nowflow,operator);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.实作.name())){
            resume=fillPracticeResult(resume,newstate,newremark,nowflow,operator);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.体检.name())){
            resume=fillPhysicalResult(resume,newstate,newremark,nowflow,operator);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.签约.name())){
            resume=fillSigningResult(resume,newstate,newremark,nowflow,operator);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.实习.name())){
            resume=fillPracticeResult(resume,newstate,newremark,nowflow,operator);
        }else if(flowName.equalsIgnoreCase(EnumTools.FlowNomalState.录用.name())){
            resume=fillExamResult(resume,newstate,newremark,nowflow,operator);
        }
        return  resume;
    }





    /**
     * 填充简历筛选结果
     * @author cyx
     * @date 2019/3/8 10:17
     * @param resume
     * @param newstate
     * @param newremark
     * @return com.jsmtr.rms.entity.DUserResume
     */
    private DUserResume fillResumeResult(DUserResume resume,String newstate,String newremark,ProjectInfoFlow nowflow,BaseUser operator){
        if (resume.getFqstate().equals("未放弃")&& resume.getFid().equals(nowflow.getId())) {
            resume.setState(newstate);
            resume.setEditDate(new Date());
            resume.setEditUserid(operator.getId());
            resume.setEditUserName(operator.getUname());
        }
        return  resume;
    }

    /**
     * 填充笔试结果
     * @author cyx
     * @date 2019/3/8 10:16
     * @param resume
     * @param newstate
     * @param newremark
     * @return com.jsmtr.rms.entity.DUserResume
     */
    private DUserResume fillExamResult(DUserResume resume,String newstate,String newremark,ProjectInfoFlow nowflow,BaseUser operator){


        return  resume;
    }


    /**
     * 填充资料审核结果
     * @author cyx
     * @date 2019/3/8 10:16
     * @param resume
     * @param newstate
     * @param newremark
     * @return com.jsmtr.rms.entity.DUserResume
     */
    private DUserResume fillCheckResult(DUserResume resume,String newstate,String newremark,ProjectInfoFlow nowflow,BaseUser operator){
        if (resume.getFqstate().equals("未放弃")&& resume.getFid().equals(nowflow.getId())) {
            resume.setZlremark(newremark);
            resume.setZlstate(newstate);
            resume.setZluid(operator.getId());
            resume.setZluname(operator.getUname());
            resume.setZldate(new Date());
        }
        return  resume;
    }

    /**
     * 填充面试结果
     * @author cyx
     * @date 2019/3/8 10:16
     * @param resume
     * @param newstate
     * @param newremark
     * @return com.jsmtr.rms.entity.DUserResume
     */
    private DUserResume fillInterviewResult(DUserResume resume,String newstate,String newremark,ProjectInfoFlow nowflow,BaseUser operator){

        return  resume;
    }

/**
 * 填充背景调查(政审)结果
 * @author cyx
 * @date 2019/3/8 10:17
 * @param resume
 * @param newstate
 * @param newremark
 * @return com.jsmtr.rms.entity.DUserResume
 */
    private DUserResume fillPReviewResult(DUserResume resume,String newstate,String newremark,ProjectInfoFlow nowflow,BaseUser operator){
        if (resume.getFqstate().equals("未放弃")&& resume.getFid().equals(nowflow.getId())) {
            resume.setZsremark(newremark);
            resume.setZsstate(newstate);
            resume.setZsuid(operator.getId());
            resume.setZsuname(operator.getUname());
            resume.setZsdate(new Date());
        }

        return  resume;
    }


    //region 填充体检结果
    /**
     * 填充体检结果
     * @author cyx
     * @date 2019/3/8 10:16
     * @param resume
     * @param newstate
     * @param newremark
     * @param nowflow
     * @param operator
     * @return com.jsmtr.rms.entity.DUserResume
     */
    private  DUserResume fillPhysicalResult(DUserResume resume, String newstate, String newremark, ProjectInfoFlow nowflow,BaseUser operator){
        if (resume.getFqstate().equals("未放弃")&& resume.getFid().equals(nowflow.getId())) {
            resume.setTjremark(newremark);
            resume.setTjstate(newstate);
            resume.setTjuid(operator.getId());
            resume.setTjuname(operator.getUname());
            resume.setTjdate(new Date());
        }
        return  resume;
    }
    //endregion

    /**
     * 记录签约结果
     * @author cyx
     * @date 2019/3/8 10:22
     * @param resume
     * @param newstate
     * @param newremark
     * @param nowflow
     * @param operator
     * @return com.jsmtr.rms.entity.DUserResume
     */
    private DUserResume fillSigningResult(DUserResume resume, String newstate, String newremark, ProjectInfoFlow nowflow,BaseUser operator){
        if (resume.getFqstate().equals("未放弃")&& resume.getFid().equals(nowflow.getId())) {
            resume.setQystate(newstate);
            resume.setQyremark(newremark);
            resume.setQyuid(operator.getId());
            resume.setQyuname(operator.getUname());
            resume.setQydate(new Date());
        }
        return  resume;
    }
    /**
     * 记录实作结果
     * @author cyx
     * @date 2019/3/8 10:20
     * @param
     * @return com.jsmtr.rms.entity.DUserResume
     */
    private DUserResume fillOperationResult(DUserResume resume,String newstate,String newremark,ProjectInfoFlow nowflow,BaseUser operator){
        if (resume.getFqstate().equals("未放弃")&& resume.getFid().equals(nowflow.getId())) {

        }
        return  resume;
    }

    /**
     * 记录实习结果
     * @author cyx
     * @date 2019/3/8 10:20
     * @param
     * @return com.jsmtr.rms.entity.DUserResume
     */
    private DUserResume fillPracticeResult(DUserResume resume,String newstate,String newremark,ProjectInfoFlow nowflow,BaseUser operator){
        if (resume.getFqstate().equals("未放弃")&& resume.getFid().equals(nowflow.getId())) {
            resume.setSxstate(newstate);
            resume.setSxbz(newremark);
            resume.setSxremark(newremark);
            resume.setSxuid(operator.getId());
            resume.setSxuname(operator.getUname());
            resume.setSxdate(new Date());
        }
        return  resume;
    }


    /**    
     * 记录录用结果
     * @author cyx  
     * @date 2019/3/8 10:20
     * @param   
     * @return com.jsmtr.rms.entity.DUserResume  
     */ 
    private DUserResume fillEmployResult(DUserResume resume,String newstate,String newremark,ProjectInfoFlow nowflow,BaseUser operator){
        if (resume.getFqstate().equals("未放弃")&& resume.getFid().equals(nowflow.getId())) {
            resume.setBdstate(newstate);
            resume.setBdremark(newremark);
            resume.setBduid(operator.getId());
            resume.setBduname(operator.getUname());
            resume.setBddate(new Date());
        }
        return  resume;
    }

}
